using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Permissions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Identity.Client;
using ShopForWeb.Model.Basic;
using ShopForWeb.Services;
using ShopForWeb.UI.Models;

namespace ShopForWeb.UI.Areas.Admin.Controllers
{
  [Area("admin")]
  [Route("[area]/[controller]")]
  public class CategoryController : Controller
  {
    private readonly ILogger<CategoryController> _logger;//依赖
    private CategoryService _categoryService;

    public CategoryController(ILogger<CategoryController> logger, CategoryService categoryService)//注入

    {
      _logger = logger;
      _categoryService = categoryService;
    }


    public async Task<IActionResult> IndexAsync([FromQuery] int statusCode=0)
    {
      //var result = await _categoryService.GetAllAsync();
      var result = await _categoryService.GetAllBySqlRawAsync(statusCode);
      return Json(result);
      // ViewBag.CategoryList = result;
      // return View();
    }
    [HttpPost]

    public async Task<IActionResult> AddNew([FromBody] Category category)
    {
      int result = await _categoryService.AddNewAsync(category);
      if (result == 1)
      {
        return Json(new ResponseViewModel()
        {
          Code = 200,
          Message = "分类添加成功"
        });
      }

      else
      {
        return Json(new ResponseViewModel()
        {
          Code = 500,
          Message = "分类添加失败"
        });
      }


    }
    [HttpPut]
    public async Task<IActionResult> Modify([FromBody] Category category)
    {
      int result = await _categoryService.ModifyAsync(category);
      if (result == 1)
      {
        return Json(new ResponseViewModel()
        {
          Code = 200,
          Message = "分类修改成功"
        });
      }

      else
      {
        return Json(new ResponseViewModel()
        {
          Code = 500,
          Message = "分类修改失败"
        });
      }


    }
    [HttpDelete("{categoryId}")]
    public async Task<IActionResult> Remove([FromRoute] int categoryId)
    {
      int result = await _categoryService.RemoveAsync(categoryId);
      if (result == 1)
      {
        return Json(new ResponseViewModel()
        {
          Code = 200,
          Message = "分类删除成功"
        });
      }

      else
      {
        return Json(new ResponseViewModel()
        {
          Code = 500,
          Message = "分类删除失败"
        });
      }



    }

  }
}